Why doesn't Python's `re.split()` split on zero-length matches?

Posted by Tim Pietzcker on Stack Overflow See other posts from Stack Overflow or by Tim Pietzcker
Published on 2010-04-26T11:34:24Z Indexed on 2010/04/26 12:03 UTC
Read the original article Hit count: 215

Filed under:
|

One particular quirk of the (otherwise quite powerful) re module in Python is that re.split() will never split a string on a zero-length match, for example if I want to split a string along word boundaries:

>>> re.split(r"\s+|\b", "Split along words, preserve punctuation!")
['Split', 'along', 'words,', 'preserve', 'punctuation!']

instead of

['', 'Split', 'along', 'words', ',', 'preserve', 'punctuation', '!']

Why does it have this limitation? Is it by design? Do other regex flavors behave like this?

© Stack Overflow or respective owner

Related posts about python

Related posts about regex